package com.quectel.core.module.entranceguard.service.impl;


import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.entranceguard.dao.VisitorDao;
import com.quectel.core.module.entranceguard.dto.VisitorDto;
import com.quectel.core.module.entranceguard.entity.VisitorEntity;
import com.quectel.core.module.entranceguard.service.VisitorService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author rananxin
 * @date 2021-12-24 13:32:07
 */
@DubboService
public class VisitorServiceImpl implements VisitorService {

    private static final Logger LOGGER = LoggerFactory.getLogger(VisitorServiceImpl.class);

    private static final String VISITOR_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "VISITOR_CACHE_BY_ID:";

    @Autowired
    private VisitorDao visitorDao;


    @Override
    public VisitorDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                VISITOR_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public VisitorDto selectById(Long id) {
        VisitorEntity entity = visitorDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, VisitorDto.class));
    }

    @Override
    public List<VisitorDto> queryList(Map<String, Object> params) {
        List<VisitorEntity> list = visitorDao.queryList(params);
        List<VisitorDto> result = CopyUtils.copyList(list, VisitorDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private VisitorDto paddingField(VisitorDto dto) {
        if (dto != null) {

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return visitorDao.queryTotal(params);
    }

    @Override
    public Long save(VisitorDto dto) {
        VisitorEntity entity = CopyUtils.copyObj(dto, VisitorEntity.class);
        entity.setId(Snowflake.nextId());
        visitorDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(VisitorDto dto) {
        VisitorEntity entity = CopyUtils.copyObj(dto, VisitorEntity.class);
        visitorDao.updateById(entity);
        CacheKit.invalidRedisCache(VISITOR_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void updateAllColumnById(VisitorDto dto) {
        VisitorEntity entity = CopyUtils.copyObj(dto, VisitorEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, visitorDao);

        CacheKit.invalidRedisCache(VISITOR_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void deleteById(Long id) {
        visitorDao.deleteById(id);

        CacheKit.invalidRedisCache(VISITOR_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
